package com.goldsprite.gameframeworks.oldinputs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;

/**
 * 平台输入类
 * 统一处理不同平台的输入（Android虚拟键盘和PC键盘）
 */
public class PlatformInput {
    // 输入状态
    private boolean leftPressed = false;
    private boolean rightPressed = false;
    private boolean upPressed = false;
    private boolean downPressed = false;
    private boolean jumpPressed = false;
    private boolean attackPressed = false;

    // 按键状态变化标记（用于检测按下和释放事件）
    private boolean leftJustPressed = false;
    private boolean rightJustPressed = false;
    private boolean upJustPressed = false;
    private boolean downJustPressed = false;
    private boolean jumpJustPressed = false;
    private boolean attackJustPressed = false;

    private boolean leftJustReleased = false;
    private boolean rightJustReleased = false;
    private boolean upJustReleased = false;
    private boolean downJustReleased = false;
    private boolean jumpJustReleased = false;
    private boolean attackJustReleased = false;

    // 单例模式
    private static PlatformInput instance;

    private PlatformInput() {}

    public static PlatformInput getInstance() {
        if (instance == null) {
            instance = new PlatformInput();
        }
        return instance;
    }

    /**
     * 更新输入状态
     * 需要在每一帧调用
	 * 仅PC
     */
    public void update() {
//        // 重置"Just"状态
//        leftJustPressed = false;
//        rightJustPressed = false;
//        upJustPressed = false;
//        downJustPressed = false;
//        jumpJustPressed = false;
//        attackJustPressed = false;
//
//        leftJustReleased = false;
//        rightJustReleased = false;
//        upJustReleased = false;
//        downJustReleased = false;
//        jumpJustReleased = false;
//        attackJustReleased = false;
//
//        // 检查PC键盘输入
//        checkKeyboardInput();
    }

    /**
     * 检查PC键盘输入
     */
    private void checkKeyboardInput() {
        // 左右移动
        boolean newLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT);
        boolean newRightPressed = Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT);
        boolean newUpPressed = Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.UP);
        boolean newDownPressed = Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DOWN);

        // 跳跃和攻击
        boolean newJumpPressed = Gdx.input.isKeyPressed(Input.Keys.SPACE) ||
                                Gdx.input.isKeyPressed(Input.Keys.W) ||
                                Gdx.input.isKeyPressed(Input.Keys.UP) ||
                                Gdx.input.isKeyPressed(Input.Keys.K);
        boolean newAttackPressed = Gdx.input.isKeyPressed(Input.Keys.J);

        // 更新状态变化标记
        if (!leftPressed && newLeftPressed) leftJustPressed = true;
        if (leftPressed && !newLeftPressed) leftJustReleased = true;
        if (!rightPressed && newRightPressed) rightJustPressed = true;
        if (rightPressed && !newRightPressed) rightJustReleased = true;
        if (!upPressed && newUpPressed) upJustPressed = true;
        if (upPressed && !newUpPressed) upJustReleased = true;
        if (!downPressed && newDownPressed) downJustPressed = true;
        if (downPressed && !newDownPressed) downJustReleased = true;
        if (!jumpPressed && newJumpPressed) jumpJustPressed = true;
        if (jumpPressed && !newJumpPressed) jumpJustReleased = true;
        if (!attackPressed && newAttackPressed) attackJustPressed = true;
        if (attackPressed && !newAttackPressed) attackJustReleased = true;

        // 更新当前状态
        leftPressed = newLeftPressed;
        rightPressed = newRightPressed;
        upPressed = newUpPressed;
        downPressed = newDownPressed;
        jumpPressed = newJumpPressed;
        attackPressed = newAttackPressed;
    }

    /**
     * 从Android虚拟按键设置输入状态
     */
    public void setKeyDown(char key) {
        switch (Character.toUpperCase(key)) {
            case 'A':
                if (!leftPressed) leftJustPressed = true;
                leftPressed = true;
                break;
            case 'D':
                if (!rightPressed) rightJustPressed = true;
                rightPressed = true;
                break;
            case 'W':
                if (!upPressed) upJustPressed = true;
                upPressed = true;
                break;
            case 'S':
                if (!downPressed) downJustPressed = true;
                downPressed = true;
                break;
            case 'J':
				if (!attackPressed) attackJustPressed = true;
				attackPressed = true;
                break;
            case 'K':
				if (!jumpPressed) jumpJustPressed = true;
				jumpPressed = true;
				System.out.println("跳跃按下");
                break;
        }
    }

    public void setKeyUp(char key) {
        switch (Character.toUpperCase(key)) {
            case 'A':
                if (leftPressed) leftJustReleased = true;
                leftPressed = false;
                break;
            case 'D':
                if (rightPressed) rightJustReleased = true;
                rightPressed = false;
                break;
            case 'W':
                if (upPressed) upJustReleased = true;
                upPressed = false;
                break;
            case 'S':
                if (downPressed) downJustReleased = true;
                downPressed = false;
                break;
            case 'J':
				if (attackPressed) attackJustReleased = true;
				attackPressed = false;
                break;
            case 'K':
				if (jumpPressed) jumpJustReleased = true;
				jumpPressed = false;
				jumpJustPressed = false;
				System.out.println("跳跃抬起");
                break;
        }
    }

    // Getter方法
    public boolean isLeftPressed() { return leftPressed; }
    public boolean isRightPressed() { return rightPressed; }
    public boolean isUpPressed() { return upPressed; }
    public boolean isDownPressed() { return downPressed; }
    public boolean isJumpPressed() { return jumpPressed; }
    public boolean isAttackPressed() { return attackPressed; }

    // JustPressed方法
    public boolean isLeftJustPressed() { return leftJustPressed; }
    public boolean isRightJustPressed() { return rightJustPressed; }
    public boolean isUpJustPressed() { return upJustPressed; }
    public boolean isDownJustPressed() { return downJustPressed; }
    public boolean isJumpJustPressed() { return jumpJustPressed; }
    public boolean isAttackJustPressed() { return attackJustPressed; }

    // JustReleased方法
    public boolean isLeftJustReleased() { return leftJustReleased; }
    public boolean isRightJustReleased() { return rightJustReleased; }
    public boolean isUpJustReleased() { return upJustReleased; }
    public boolean isDownJustReleased() { return downJustReleased; }
    public boolean isJumpJustReleased() { return jumpJustReleased; }
    public boolean isAttackJustReleased() { return attackJustReleased; }
}
